<template>
  <el-card>
    <template #header>
      <div>
        工作日历
      </div>
    </template>
    <div style="text-align:right">
      <el-select v-model="year" @change="changeEvent">
        <el-option
          v-for="item in yearArr"
          :key="item"
          :value="item"
          :label="item"
        ></el-option>
      </el-select>
      <el-select v-model="month" @change="changeEvent">
        <el-option
          v-for="item in monthArr"
          :key="item"
          :value="item"
          :label="item"
        ></el-option>
      </el-select>
    </div>
    <!-- 日历组件 -->
    <el-calendar v-model="value">
      <template #dateCell="{data,date}">
        <div
          @click="cellClick(date)"
          class=""
          style="width:100%;height:100%,text-align:center"
        >
          <span>{{ data.day.split('-')[2] }}</span>
          <span v-if="formateTime(date) && data.type === 'current-month'"
            >休</span
          >
        </div>
      </template>
    </el-calendar>
  </el-card>
</template>

<script>
export default {
  props: {
    calendarTime: {
      type: Date,
      default: () => {
        return new Date()
      }
    }
  },
  model: { prop: 'calendarTime' },
  watch: {
    // 监听父组件传来的时间的变化
    calendarTime: {
      deep: true,
      immediate: true,
      handler (newVal) {
        this.value = newVal
        this.year = newVal.getFullYear()
        this.month = newVal.getMonth() + 1
        // 年的时间范围
        let min = this.year - 5
        let max = this.year + 5
        let newYearArr = []
        for (let i = min; i < max; i++) {
          newYearArr.push(i)
        }
        this.yearArr = newYearArr
      }
    }
  },
  data () {
    return {
      value: new Date(),
      year: '',
      yearArr: [],
      month: '',
      monthArr: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    }
  },
  methods: {
    // 自定义日历组件格式变化
    formateTime (time) {
      if (time.getDay() === 0 || time.getDay() === 6) {
        return true
      } else {
        return false
      }
    },
    // 点击选择框
    changeEvent () {
      this.$emit('input', new Date(this.year + '-' + this.month + '-01'))
    },
    // 点击日历组件
    cellClick (date) {
      console.log(date)
      this.$emit('input', date)
    }
  }
}
</script>

<style></style>
